import tkinter as tk
import tkinter.messagebox

# 创建窗体
win = tk.Tk()
# 设置标题
win.title("我是第一个gui程序")
users = ["root", "admin"]
pwds = ['123', '111']


# 登录事件
def longin():
    u = var_user.get()  # 获取输入的数据
    p = var_pwd.get()
    if u in users:
        index1 = users.index(u)  # 找到索引
        index2 = pwds[index1]  # 根据索引找到对应的密码
        if index2 == p:
            tk.messagebox.showinfo("欢迎登录！！！", message=u)
        else:
            tk.messagebox.showerror("err", message="登录失败，密码不匹配")
    else:
        tk.messagebox.showerror("err", message="账号不存在")


def exits():
    win.destroy()


# 设置窗体大小于位置
win.geometry('500x400')
# 窗口布局
"""
fg 字体颜色
bg 背景颜色
place 标签框位置
"""
# 布局 1.绝对布局
tk.Label(win, text="用户名:", font=("宋体", 25), fg='red').place(x=10, y=10)
tk.Label(win, text="密 码:", font=("宋体", 25), fg='red').place(x=10, y=60)
# 文本框
var_user = tk.StringVar()
user = tk.Entry(win, textvariable=var_user, font=("黑体", 25)).place(x=130, y=10)
# 密码框
var_pwd = tk.StringVar()
pwd = tk.Entry(win, textvariable=var_pwd, show="*", font=("宋体", 15)).place(x=120, y=60)
# 按钮 绝对布局
tk.Button(win, text="登录",command=longin, font=("楷体", 20)).place(x=120, y=120)
tk.Button(win, text="注销",command=exits, font=("楷体", 20)).place(x=200, y=120)
"""------------------------------------------------------"""
# # 2.相对布局 fill=tk.Y[纵向填充],side=tk.LEFT[左对齐]
# tk.Label(win, text="我是文本", font=("宋体", 25), fg='red', bg="green").pack(fill=tk.Y, side=tk.LEFT)
"""------------------------------------------------------"""
# #  3.表格布局 grid(row=行数, column=列数)
# tk.Label(win, text="我是文本", font=("宋体", 25), fg='red', bg="green").grid(row=0, column=0)
# tk.Label(win, text="我是文本", font=("宋体", 25), fg='red', bg="green").grid(row=1, column=1)
# 执行窗体
win.mainloop()
